# -*- coding: utf-8 -*-
# @Time    : 2018/6/7 下午10:07
# @Author  : Lavn
# @Email   : 959875529@qq.com
# @File    : threadDemo.py
# @Software: PyCharm

import time
import threading

#主线程结束后 自线程还在运行
# class MyThread(threading.Thread):
#     def run(self):
#         for i in range(5):
#             print('thread-{}, @number: {}'.format(self.name, i))
#             time.sleep(1)
#
# def main():
#     print("Start main threading")
#
#     # 创建三个线程
#     threads = [MyThread() for i in range(3)]
#     # 启动三个线程
#     for t in threads:
#         t.start()
#
#     print("End Main threading")
#
#
# if __name__ == '__main__':
#     main()

#子线程运行完 才运行主线程
# import time
# import threading
#
#
# class MyThread(threading.Thread):
#     def run(self):
#         for i in range(5):
#             print('thread {}, @number: {}'.format(self.name, i))
#             time.sleep(1)
#
#
# def main():
#     print("Start main threading")
#
#     # 创建三个线程
#     threads = [MyThread() for i in range(3)]
#     # 启动三个线程
#     for t in threads:
#         t.start()
#
#     # 一次让新创建的线程执行 join
#     for t in threads:
#         t.join()
#
#     print("End Main threading")
#
#
# if __name__ == '__main__':
#     main()

# import threading, time
# class Consumer(threading.Thread):
#     def __init__(self, cond, name):
#         # 初始化
#         super(Consumer, self).__init__()
#         self.cond = cond
#         self.name = name
#
#     def run(self):
#         # 确保先运行Seeker中的方法
#         time.sleep(1)
#         self.cond.acquire()
#         print(self.name + ': 我这两件商品一起买，可以便宜点吗')
#         self.cond.notify()
#         self.cond.wait()
#         print(self.name + ': 我已经提交订单了，你修改下价格')
#         self.cond.notify()
#         self.cond.wait()
#         print(self.name + ': 收到，我支付成功了')
#         self.cond.notify()
#         self.cond.release()
#         print(self.name + ': 等待收货')
#
#
# class Producer(threading.Thread):
#     def __init__(self, cond, name):
#         super(Producer, self).__init__()
#         self.cond = cond
#         self.name = name
#
#     def run(self):
#         self.cond.acquire()
#         # 释放对琐的占用，同时线程挂起在这里，直到被 notify 并重新占有琐。
#         self.cond.wait()
#         print(self.name + ': 可以的，你提交订单吧')
#         self.cond.notify()
#         self.cond.wait()
#         print(self.name + ': 好了，已经修改了')
#         self.cond.notify()
#         self.cond.wait()
#         print(self.name + ': 嗯，收款成功，马上给你发货')
#         self.cond.release()
#         print(self.name + ': 发货商品')
#
#
# cond = threading.Condition()
# consumer = Consumer(cond, '买家（两点水）')
# producer = Producer(cond, '卖家（三点水）')
# consumer.start()
# producer.start()


#线程间通信
from queue import Queue
from threading import Thread

isRead = True


def write(q):
    # 写数据进程
    for value in ['两点水', '三点水', '四点水']:
        print('写进 Queue 的值为：{0}'.format(value))
        q.put(value)


def read(q):
    # 读取数据进程
    while isRead:
        value = q.get(True)
        print('从 Queue 读取的值为：{0}'.format(value))


if __name__ == '__main__':
    q = Queue()
    t1 = Thread(target=write, args=(q,))
    t2 = Thread(target=read, args=(q,))
    t1.start()
    t2.start()